package com.cosina.game.crystallight.ui.menu;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;

import com.cosina.game.crystallight.CrystalLightActivity;
import com.cosina.game.crystallight.ui.ResourceManager;
import com.cosina.game.crystallight.ui.ResourceReference;
import com.cosina.game.crystallight.ui.menu.util.ViewPart;

public abstract class CompositeView extends View {
	protected CrystalLightActivity activity;
	protected List<ViewPart> viewParts = new ArrayList<ViewPart>();
	public int partXSpan = 0;
	public int partYSpan = 0;
	
	public CompositeView(CrystalLightActivity activity) {
		super(activity);
		this.activity = activity;
		setBackgroundDrawable(new BitmapDrawable(ResourceManager.get(ResourceReference.IMG_MENU_MAIN)));
	}
	
	public abstract void onButtonClicked(int id);
	
	@Override
	protected void onDraw(Canvas canvas) {
		for(ViewPart each : viewParts){
			each.draw(canvas);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			int x = (int) event.getX() - partXSpan;
			int y = (int) event.getY() - partYSpan;
			for (ViewPart binfo : viewParts) {
				if (binfo.containPoint(x, y)) {
					binfo.click();
					break;
				}
			}
			return true;
		}
		return super.onTouchEvent(event);
	}
}
